package cn.tp.ksoft.civil_driver.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.OrderBean;
import cn.tp.ksoft.civil_driver.bean.OrderDetailBean;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.dialog.ConfirmDurationDialog;
import cn.tp.ksoft.civil_driver.dialog.DialogCallBack;
import cn.tp.ksoft.civil_driver.request.DriverOrderRequest;
import cn.tp.ksoft.civil_driver.utils.Constants;

/**
 * Created by zhusong on 16/5/2.
 */
public class OrderConfirmActivity extends DriverBaseActivity implements View.OnClickListener{

    private ConfirmDurationDialog mDialog;
    private TextView mDayTv;
    private TextView mHourTv;
    private TextView mPriceTv;
    private OrderBean orderBean;

    private DriverOrderRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_confirm_order);
        updateTitle(getString(R.string.driver_order_confirm_duration));
        request = new DriverOrderRequest(this, new WeakHandler(this));
        findViewById(R.id.ll_duration).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        mDayTv = (TextView) findViewById(R.id.tv_day);
        mHourTv = (TextView) findViewById(R.id.tv_hour);
        mPriceTv = (TextView) findViewById(R.id.tv_price);
        mDialog = new ConfirmDurationDialog(this);
        mDialog.setCallBack(new DialogCallBack() {
            @Override
            public void onPositiveClick(Object... result) {
                mDayTv.setText(result[0].toString());
                mHourTv.setText(result[1].toString());
            }

            @Override
            public void onNegativeClick(Object... result) {

            }
        });

        Intent intent = getIntent();
        if (null != intent) {
            orderBean = (OrderBean) intent.getSerializableExtra(Constants.KEY_ORDER_BEAN);
            OrderDetailBean detailBean = orderBean.getDetailBean();
            int hours = detailBean.getBycarLong();
            int day = hours / 8;
            int hour = hours % 8;
            mDayTv.setText(String.valueOf(day));
            mHourTv.setText(String.valueOf(hour));
            mPriceTv.setText("¥"+orderBean.getOrderMoney());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_duration : {
                mDialog.show();
                break;
            }
            case R.id.btn_ok : {
                int day = Integer.parseInt(mDayTv.getText().toString());
                int hour = Integer.parseInt(mHourTv.getText().toString());
                request.finishOrder(orderBean.getId(), day * 8 + hour, orderBean.getDiscountMoney());
                break;
            }
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        setResult(RESULT_OK);
        finish();
    }
}
